package com.ruoyi.controller;

import java.util.Objects;
import java.time.Instant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.HhbUser;
import com.ruoyi.service.HhbUserService;

import javax.annotation.Resource;

/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/system/hhb/user")
public class HhbUserController extends BaseController
{
    @Autowired
    private HhbUserService userService;


    @Resource
    private PasswordEncoder passwordEncoder;
    /**
     * 查询用户列表
     */
    @PostMapping("/list")
    public AjaxResult page(@RequestBody Page<HhbUser> page, @RequestBody HhbUser user){
//        System.out.println(page.getSize() + "ssssssssssssss" + page.getCurrent() + audit);

        String encode = passwordEncoder.encode("123456");

        System.out.println(encode + "这是我加密的密码");

        //$2a$10$3.oVWzoLnMzjyTpa86hlt.IxPgNgdEgCVfzEHFZdNizR.4OFkg3dW
        //$2a$10$9Aw9uJNtPfrw8vyhY59W6ezF2GJOYpgyuuvgRxXBxsgk0ZGVl7AaO

        userService.page(page,new LambdaQueryWrapper<HhbUser>()
                //内容 eq = like '%%'
                .like(StringUtils.isNotBlank(user.getUsername()), HhbUser::getUsername,user.getUsername())
                .like(StringUtils.isNotBlank(user.getNickname()), HhbUser::getNickname,user.getNickname())
                .eq(Objects.nonNull(user.getId()), HhbUser::getId,user.getId())
                .eq(Objects.nonNull(user.getMobile()), HhbUser::getMobile,user.getMobile())
                .eq(Objects.nonNull(user.getCode()), HhbUser::getCode,user.getCode())
                .eq(HhbUser::getDeleteFlag,Boolean.FALSE)
        );

        //设置总行数
        page.setTotal(userService.count(new LambdaQueryWrapper<HhbUser>()
                .like(StringUtils.isNotBlank(user.getUsername()), HhbUser::getUsername,user.getUsername())
                .like(StringUtils.isNotBlank(user.getNickname()), HhbUser::getNickname,user.getNickname())
                .eq(Objects.nonNull(user.getId()), HhbUser::getId,user.getId())
                .eq(Objects.nonNull(user.getMobile()), HhbUser::getMobile,user.getMobile())
                .eq(Objects.nonNull(user.getCode()), HhbUser::getCode,user.getCode())
                .eq(HhbUser::getDeleteFlag,Boolean.FALSE)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    /**
     * 新增用户
     */
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HhbUser user)
    {
        user.setCreateBy(SecurityUtils.getUsername());
        user.setCreateTime(Instant.now().getEpochSecond());
        user.setUpdateBy(SecurityUtils.getUsername());
        user.setUpdateTime(Instant.now().getEpochSecond());
        user.setDeleteFlag(Boolean.FALSE);
        return toAjax(userService.save(user));
    }

    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody HhbUser user)
    {
        user.setUpdateBy(SecurityUtils.getUsername());
        user.setUpdateTime(Instant.now().getEpochSecond());
        return toAjax(userService.updateById(user));
    }

    /**
     * 删除用户
     */
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id)
    {

        return toAjax(userService.removeById(id));
    }
}
